/*12/14/09 19:02
*
*Cha Li
*CS4804 - Final Project
*Developed in Ubuntu 9.04 x64
*Compiled using g++
*
*/

#include <cmath>
#include <iostream>
#include "Square.h"
#define NUM_EDGES 4
using namespace std;

/*private*/
void Square::setEdges(int m, int n, int h_start){
	edge_arr[0] = index + i_row; 
	edge_arr[1] = edge_arr[0] + 1;
	edge_arr[2] = index + h_start + (m * i_col) - ((n - 1) * i_row ); 
	edge_arr[3] = edge_arr[2] + 1;
}

/*public*/
Square::Square(){
}

// h_start is the index of the first horizontal edge in the array
bool Square::initSquare(int i, int rows, int cols, int h_start){
	index = i;
	edge_arr = new int[NUM_EDGES];
	edges_filled = 0;
	i_row = index / cols;
	i_col = index % cols;
	setEdges(rows, cols, h_start);	

return true;

}

int Square::getWorth(int state){
	calcFilled(state);	
	switch(edges_filled){
		case 0:
			return 31;
		case 1:
			return 23;
		case 2:
			return 1;
		case 3:
			return 201;
		}
}

bool Square::containsEdge(int index){
	int i;	
	for(i = 0; i < NUM_EDGES; i++){
		if(edge_arr[i] == index)
			return true;
	}
	return false;
}

int Square::calcFilled(int state){
	int i;
	//reset value for every calculation
	edges_filled = 0;
	for(i = 0; i < NUM_EDGES; i++)
		if((int)pow(2,edge_arr[i]) & state)
			edges_filled++;

	return edges_filled;
}

ostream& operator<< (ostream& out, Square sqr){
	out << "Linear Index: " << sqr.index << endl;
	out << "Row: " << sqr.i_row << endl;
	out << "Col: " << sqr.i_col << endl;
	out << sqr.edge_arr[0] << " " << sqr.edge_arr[1] <<  " " << sqr.edge_arr[2] <<  " " << sqr.edge_arr[3] << endl;
  return out;
}

